package com.biglead.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biglead.admin.api.CommonPage;
import com.biglead.admin.api.CommonResult;
import com.biglead.admin.pojo.UmsMenu;
import com.biglead.admin.pojo.UmsMenuNode;
import com.biglead.admin.service.UmsMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin/menu")
public class AdminMenuController {
    @Autowired
    private UmsMenuService menuService;

    /**
     * 添加后台菜单
     * @param umsMenu
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody UmsMenu umsMenu) {
        boolean count = menuService.create(umsMenu);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据ID获取菜单详情
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    @ResponseBody
    public CommonResult<UmsMenu> getItem(@PathVariable Long id) {
        UmsMenu umsMenu = menuService.getItem(id);
        return CommonResult.success(umsMenu);
    }

    /**
     * 修改后台菜单
     * @param id
     * @param umsMenu
     * @return
     */
    @PostMapping(value = "/update/{id}")
    @ResponseBody
    public CommonResult update(@PathVariable Long id,
                               @RequestBody UmsMenu umsMenu) {
        boolean count = menuService.update(id, umsMenu);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据ID删除后台菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean count = menuService.delete(id);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     * 分页查询后台菜单
     * @param parentId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping(value = "/list/{parentId}")
    @ResponseBody
    public CommonResult<Page<UmsMenu>> list(@PathVariable Long parentId,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<UmsMenu> list = menuService.list(parentId, pageSize, pageNum);
        return CommonResult.success(list);
    }

    /**
     * 树形结构返回所有菜单列表
     * @return
     */
    @GetMapping(value = "/treeList")
    @ResponseBody
    public CommonResult<List<UmsMenuNode>> treeList() {
        List<UmsMenuNode> list = menuService.treeList();
        return CommonResult.success(list);
    }

    /**
     * 修改菜单显示状态
     * @param id
     * @param hidden
     * @return
     */
    @PostMapping(value = "/updateHidden/{id}")
    @ResponseBody
    public CommonResult updateHidden(@PathVariable Long id, @RequestParam("hidden") Integer hidden) {
        boolean count = menuService.updateHidden(id, hidden);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
